﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;

public class datYKien
{
    SqlCommand cmd;
    SqlDataReader dr;
    public List<entYKien> getAll()
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_YKienGetAll", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        dr = cmd.ExecuteReader();
        entYKien entYK = new entYKien();
        List<entYKien> lst = new List<entYKien>();
        while (dr.Read())
        {
            entYK = new entYKien();
            entYK.YKienId = int.Parse(dr["YKienId"].ToString());
            entYK.ChuDe = dr["ChuDe"].ToString();
            entYK.NoiDung = dr["NoiDung"].ToString();
            entYK.NguoiGui = dr["NguoiGui"].ToString();
            if (dr["nguoidoc"].ToString() != "")
            {
                entYK.NguoiDoc = int.Parse(dr["NguoiDoc"].ToString());
            }
            entYK.TrangThai = int.Parse(dr["TrangThai"].ToString());
            lst.Add(entYK);
        }
        dr.Close();
        sqlConnection.Close();
        return lst;
    }
    public entYKien getbyid(int id, int nguoidoc)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_YKienGetByid", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[2];
        sqla[0] = new SqlParameter("YKienId", DbType.Int32);
        sqla[1] = new SqlParameter("NguoiDoc", DbType.Int32);
        sqla[0].Value = id;
        sqla[1].Value = nguoidoc;
        cmd.Parameters.AddRange(sqla);
        dr = cmd.ExecuteReader();
        entYKien entYK = new entYKien();
        if (dr.Read())
        {
            entYK = new entYKien();
            entYK.YKienId = int.Parse(dr["YKienId"].ToString());
            entYK.ChuDe = dr["ChuDe"].ToString();
            entYK.NoiDung = dr["NoiDung"].ToString();
            entYK.NguoiGui = dr["NguoiGui"].ToString();
            if (dr["nguoidoc"].ToString() != "")
            {
                entYK.NguoiDoc = int.Parse(dr["NguoiDoc"].ToString());
            }
            entYK.TrangThai = int.Parse(dr["TrangThai"].ToString());
        }
        dr.Close();
        sqlConnection.Close();
        return entYK;
    }
    public void create(entYKien entYK)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_YKienCreate", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[3];
        sqla[0] = new SqlParameter("chude", DbType.String);
        sqla[1] = new SqlParameter("noidung", DbType.String);
        sqla[2] = new SqlParameter("nguoiGui", DbType.String);

        sqla[0].Value = entYK.ChuDe;
        sqla[1].Value = entYK.NoiDung;
        sqla[2].Value = entYK.NguoiGui;
        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
    }
}

